//-----------------------------------------------------------------------
// <copyright file="Helper.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Giles Knap [Updated 03 Apr 08: Mike Fourie - Code Cleanup, TFS 2008 Support, Workingdirectory Support etc.]</author>
// <date>2006-03-23</date>
// <summary>Helper functions for SourceTfs tasks</summary>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.SourceTfs
{
    using System;
    using System.IO;
    using System.Collections.Generic;

    /// <summary>
    /// Helper functions for SourceTfs tasks
    /// </summary>
    public class Helper
    {
        private static string tfexePath = "tf.exe";

        /// <summary>
        /// Initializes a new instance of the <see cref="Helper"/> class.
        /// </summary>
        /// <param name="tfsVersion">The TFS version.</param>
        public Helper(string tfsVersion)
        {
            string vstools = "";
            switch (tfsVersion)
            {
                case "2005":
                    vstools = Environment.GetEnvironmentVariable("VS80COMNTOOLS");
                    break;
                case "2008":
                    vstools = Environment.GetEnvironmentVariable("VS90COMNTOOLS");
                    break;
            }

            if (vstools != null && vstools.Length > 0)
            {
                tfexePath = Path.Combine(vstools, @"..\IDE\tf.exe");
            }

            // if tfExePath does not exist we fail over to just tf.exe and hope its in the path environment variable
            if (File.Exists(tfexePath) == false)
            {
                tfexePath = "tf.exe";
            }
        }

        /// <summary>
        /// Gets the tf exe path.
        /// </summary>
        /// <value>The tf exe path.</value>
        public string TfExePath
        {
            get { return tfexePath; }
        }

        /// <summary>
        /// Finds the last line.
        /// </summary>
        /// <param name="fullOutput">The full output.</param>
        /// <returns>string</returns>
        public static string FindLastLine(string fullOutput)
        {
            string[] linesArr = fullOutput.Split('\n');
            return linesArr[linesArr.Length - 2];
        }

        /// <summary>
        /// Splits the columns.
        /// </summary>
        /// <param name="line">The line.</param>
        /// <returns>string</returns>
        public static string[] SplitColumns(string line)
        {
            List<string> items = new List<string>();
            int pos = 0;

            while (pos != -1)
            {
                int itemstart = pos;
                pos = line.IndexOf(' ', pos + 1);

                if (pos == -1)
                {
                    items.Add(line.Substring(itemstart));
                }
                else
                {
                    items.Add(line.Substring(itemstart, pos - itemstart));
                    while (pos < line.Length && line[pos] == ' ')
                    {
                        pos++;
                    }
                }
            }

            return items.ToArray();
        }
    }
}